let prex = 0;
let prey = 0;
//获取接触屏幕时的X和Y
let startPageX = 0;
let startPageY = 0; 
const app = getApp()
var offsetYNum=0;
var offsetXNum=0;
let animation = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-in',
}); 
Component({
  properties: {  
      userInfo: {
        type: Object,
        value: {},  
      }, 
      userDataList:{
        type: Array,
        value: [], 
        observer: function(newData,oldData){ 
          // console.log(newData);
          let vm = this;  
          let objAttr='';
          let roller=vm.data.roller;
          newData.forEach(function(item,index){  
            objAttr=item.ruuid;
            vm.setData({
              ['roller.'+item.ruuid]:{ x:0, y:0 },  
            }) 
          })   
          vm.setData({
            userImgList:newData[0].photo?newData[0].photo:[], 
          })   
        },  
      }, 
      //当前显示的用户ruuid
      rollerAttr: {
        type: String,
        value: '',  
        observer:function(){ 
          let vm = this;
          setTimeout(function(){
            vm.setData({
              userRuuid:'', 
            }) 
          },100) 
        }, 
      }, 
      superlikeNum: {
        type: Number,
        observer:function(newVal,oldVal){ 
          let vm =this; 
          if(newVal>=0){
            vm.setData({
              userRuuid:vm.data.rollerAttr,
            }) 
          }
        }  
      },
      likeNumNull: {
        type: Number, 
        observer:function(newVal,oldVal){ 
          let vm =this;  
          if(newVal>=0){
            setTimeout(function(){
              vm.setData({
                userRuuid:vm.data.rollerAttr, 
              }) 
            },100)  
          }else{
            vm.setData({
              userRuuid:'', 
            }) 
          }
        }  
      },
      isSelect:{ 
        type: Number, 
        observer: 'isFunSelect'
      },
      isHint:{
        type: Boolean,
        value: false,
        observer: 'isHintFun'
      },
       
  },
  // 启用插槽
  options: {
    multipleSlots: true
  },
  data: { 
    userImgList:[],  //当前用户的相册列表
    
    roller:{},  
    userRuuid:'',//当前滑动过去的用户urrid
    hiddenimg: true,
    pagePadding:136,
    screenHeightHalf:null,   
    current:0, 
    offLeft:false, //轮播到第一张的动画 
    offRight:false, //轮播到最后的动画
    hintImg:false, //操作提示
    selectOutIcon:0,  //跳过的标签
    selectLikeIcon:0,  //喜欢的标签
    selectSuperLikeIcon:0, //超级喜欢的标签
    //图片倾斜角度
    imgDeg:0,
    imgOriginX:0, 
    //底部图片联动
    nextX:null,
    nexty:null, 
    //下一个
    swiperOffset:0,
    superLikeTransform:false,  //superLike动画
    //底部操作按钮
    tsfDislike:1,
    tsfSuperLike:1,
    tsfLike:1,
    btnScaleIndex:0,
    btnScaleShow:true, 
    twoImgLink:'', 
    swiperAnimation:{},
    rollerX:0,
    rollerY:0,
    windowWidth:wx.getStorageSync('systemInfo').windowWidth,
    windowHeight:wx.getStorageSync('systemInfo').windowHeight,

    clickTimeout:true,  //操作按钮延迟（防止连点）
  }, 
  //初始化数据
  attached(){  
    this.setData({
      pagePadding:wx.getStorageSync('headHeight'), 
      screenHeightHalf:wx.getStorageSync('systemInfo').screenHeight/2, 
    })   
  },
  created() { 
    let vm = this;  
    setTimeout(function(){
      vm.setData({
        btnScaleShow:false
      }) 
    },200) 
    
  },
    /**
   * 组件的方法列表
   */
  methods: { 
    //监听轮播图
    bindchange:function(e){  
      // this.current=e.detail.current; 
      this.setData({
          current:e.detail.current 
      }) 
    },
    swiperClis:function(e){
      let gidVal=e.currentTarget.dataset.gid; 
      let dataCurrent=this.data.current;
      let vm = this;
      // console.log(dataCurrent);
      if(gidVal=='down'){
        if(dataCurrent==0){
          wx.vibrateShort(); 
          vm.setData({
              current:0,
              offLeft:true
          }) 
          setTimeout(function(){ 
            vm.setData({
              offLeft:false
            })
          },200);
        }else{ 
          dataCurrent--;
          vm.setData({
            current:dataCurrent
          }) 
        }
      }else{ 
        if(dataCurrent==vm.data.userImgList.length-1){ 
          wx.vibrateShort();
          let imgLength=vm.data.userImgList.length-1;
          vm.setData({
              current:imgLength,
              offRight:true
          }) 
          setTimeout(function(){ 
            vm.setData({
              offRight:false
            })
          },200);
        }else{
          dataCurrent++;
          vm.setData({
            current:dataCurrent
          }) 
        } 
      }
    },
    //打开详情
    openDetail(e){ 
      let eventDetail={
            ruuid:e.currentTarget.dataset.ruuid,
            current:this.data.current,
        };
      this.triggerEvent('openDetail',eventDetail);
    }, 
    //点击
    clickSelect(e){  
      let vm = this; 
      if(vm.data.clickTimeout){
        vm.setData({
          clickTimeout:false,
        },function(){
          setTimeout(function(){
            vm.setData({
              clickTimeout:true,
            })
          },800)
        })
        let indexValue=e.currentTarget.dataset.value;  
        let userItem={}; 
        vm.data.userDataList.forEach((item,index)=>{
          if(item.ruuid==vm.data.rollerAttr){
            userItem=item
          }
        })
        let eventDetail={
          index:indexValue,
          list:userItem,
        };
        // 传递事件
        vm.triggerEvent('changeSelect',eventDetail);
        vm.setData({
          hintImg:false,
        })
        if(indexValue==4||indexValue==5){
          vm.setData({
            btnScaleIndex:indexValue
          }); 
          setTimeout(function(){
            vm.setData({
              btnScaleIndex:0
            });
          },300)
        }else{ 
          vm.offsetImage(indexValue);
        }  
      } 
    },
    offsetImage(index){ 
      let vm = this; 
      vm.animation = animation;
      if(index==1){  
        vm.animation.translateX(1000).translateY(0).step();
      }else if(index==2){
        vm.animation.translateX(-1000).translateY(0).step();
      }else{
        vm.animation.translateY(-1000).translateX(0).step();
        console.log(vm.data.superlikeNum);
        // console.log(vm.data.userDataList);
        // console.log(vm.data.rollerAttr);
      }
      vm.animation.translateY(0).translateX(0).step(); 
      vm.setData({
        swiperAnimation: vm.animation.export(), 
        tsfDislike:index==2?.6:1,
        tsfSuperLike:index==3?.6:1,
        tsfLike:index==1?.6:1, 
        selectOutIcon:index==2?1:0,
        selectSuperLikeIcon:index==3?1:0,
        selectLikeIcon:index==1?1:0,   
          
      })  
      setTimeout(function () {
        prex = 0;
        prey = 0; 
        if(index==2){
          vm.setData({
            userRuuid:vm.data.rollerAttr, 
          },function(){
          //  setTimeout(function(){
              
          //   },300); 
          })
        }
        vm.setData({
          // swiperOffset:0, 
          superLikeTransform:index==3?true:false,  
          tsfDislike:1,
          tsfSuperLike:1,
          tsfLike:1,
          selectOutIcon:0,
          selectSuperLikeIcon:0,
          selectLikeIcon:0, 
          current:0,   
        },function(){
          vm.setData({
            swiperAnimation:{}
          })
        }) 
      }, 200)
      setTimeout(function(){
        vm.setData({
          superLikeTransform:false,
        })
      },1000)
    },
    isFunSelect(newval, oldval){ 
      let vm = this; 
      setTimeout(function(){
        vm.offsetImage(newval); 
      },200) 
    },
    isHintFun(){
      this.setData({
        hintImg:this.data.isHint
      })
    },
    closeHintImg(){ 
      this.triggerEvent('isHintFun',false);
    },
    learnHintImg(){
      // this.triggerEvent('isHintFun',true); 
    },
  }
 
})
